﻿using System;
using System.Collections.Generic;
////using System.Linq;
using System.Text;
using System.IO;

namespace DBSourceToolsLib.FileComparer
{
    public class FileComparer
    {
        private string m_sourceFileName;
        private string m_targetFileName;

        public FileComparer(string strSourceFileFullPath, string strTargetFileFullPath)
        {
            m_sourceFileName = strSourceFileFullPath;
            m_targetFileName = strTargetFileFullPath;
        }

        public bool DoFilesExist()
        {
            return (SourceFileExists() && TargetFileExists());
        }
        public bool AreFilesEqual()
        {
            if (!DoFilesExist())
                return false;
            bool areFilesEqual = true;

            using (StreamReader sourceStream = new StreamReader(m_sourceFileName))
            {
                using (StreamReader targetStream = new StreamReader(m_targetFileName))
                {
                    string sourceLine;
                    string targetLine;
                    bool stillReading = true;
                    while (stillReading && areFilesEqual) 
                    {
                        sourceLine = sourceStream.ReadLine();
                        targetLine = targetStream.ReadLine();
                        if ((sourceLine != null) && (targetLine != null))
                        {
                            areFilesEqual = (sourceLine.CompareTo(targetLine) == 0);
                        }
                        if ((sourceLine == null) || (targetLine == null))
                            stillReading = false;
                        if ((sourceLine == null) && (targetLine != null))
                            areFilesEqual = false;
                        if ((targetLine == null) && (sourceLine != null))
                            areFilesEqual = false;
                    }

                }
            }
            return areFilesEqual;
        }

        public bool SourceFileExists()
        {
            return (System.IO.File.Exists(m_sourceFileName));
        }
        public bool TargetFileExists()
        {
            return (System.IO.File.Exists(m_targetFileName));
        }
    }
}
